<%--
   help
   Copyright (c) 2013 ISPERP
   Version : 1.0.0
 --%>
<%@ tag language="java" pageEncoding="UTF-8"
	deferredSyntaxAllowedAsLiteral="false" trimDirectiveWhitespaces="true"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="isperp" uri="/WEB-INF/tlds/isperp.tld"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ attribute name="relationId" required="true" type="java.lang.String"
	description="relationId"%>
<%@ attribute name="confCategoryId" required="true"
	type="java.lang.String" description="confCategoryId"%>
<%@ attribute name="id" required="true" type="java.lang.String"
	description="id"%>
<%@ attribute name="viewOnly" required="flase" type="java.lang.String"
	description="viewOnly"%>
<style>
.m-tab-trigger {
	float: left;
	height: 31px;
	margin-bottom: -1px;
}

.m-tab-trigger .trig-item {
	float: left;
	height: 30px;
}

li {
	display: list-item;
	text-align: -webkit-match-parent;
}

.m-tab-trigger .trig-item a {
	display: inline-block;
	padding: 0 10px;
	font-family: "microsoft yahei";
	font-size: 14px;
}

.m-tab-trigger-wrap {
	position: relative;
	height: 30px;
	line-height: 30px;
	margin-top: 4px;
	border-right: 1px solid #DEDFDE;
	border-bottom: 1px solid #DEDFDE;
	border-left: 1px solid #DEDFDE;
	background-color: #F7F7F7;
	overflow: visible;
}

#pro-detail-hd {
	position: relative;
	overflow: visible;
	background-color: #fff;
	z-index: 2;
}

.m-tab-trigger .trig-item.curr a {
	position: relative;
	height: 35px;
	line-height: 35px;
	margin-top: -6px;
	margin-left: -1px;
	border-left: 1px solid #DEDFDE;
	border-right: 1px solid #DEDFDE;
	background-color: #fff;
}

a {
	color: #666;
	text-decoration: underline;
}

.itmecurr {
	color: #CC3333;
}
</style>

<div id="relateCiTab" class="box ">
	<input type="text" style="display:none" id="${id}relationTypeId">
	<div class="mt J-detail-tab" id="pro-detail-hd"
		data-fixed="pro-detail-hd-fixed">
		<c:if test="${(viewOnly eq false) or (empty viewOnly) }">
		<div class="mt-inner m-tab-trigger-wrap clearfix">
			<ul class="m-tab-trigger" id="${id}_tabLiContain">

			</ul>
			
				<div style="float:right;">
					<label>请选择关联资产类型：</label> <select id="${id}drpRelationType"
						style="width:260px;">
					</select>
					<button type="button" onclick="${id}_addRelation()">新增</button>
				</div>
			
		</div>
      </c:if>
	</div>
	<div style="padding:0" class="content tabbed">
		<div id="basicInfo1" style="">
			<c:choose>
				<c:when test="${(viewOnly eq false) or (empty viewOnly) }">
					<isperp:grid url="" autoload="false" gridId="${id }_ciGrid"
						altRows="true" pager="servicePager" cusFormatterActions="true"
						ondbClickRowEvent="" actionsWidth="90" showToolbar="false"
						cusDeleteUrl="configuration/ciRelation/delete"
						cusDialogSize="normal" showSearchbar="false" showPager="false">
						<jsp:attribute name="searchbar">
				</jsp:attribute>
						<jsp:body>
				<isperp:column name="id" title="id" hidden="true"></isperp:column>
				<isperp:column name="ci_id" title="ci_id" hidden="true"></isperp:column>
				<isperp:column name="code" title="编号" width="60"
								formatter="return formatCi(cellvalue,options,rowObject);"
								isFormatterFun="true">
				 </isperp:column>
				<isperp:column name="name" title="名称" width="70">
				</isperp:column><isperp:column name="relation_property" title="连接属性" width="180">
				</isperp:column>
				<isperp:column name="treenode_name_path" title="类别" width="90">
				</isperp:column>
			       </jsp:body>
					</isperp:grid>
				</c:when>
				<c:otherwise>
					<isperp:grid url="" autoload="false" gridId="${id }_ciGrid"
						altRows="true" pager="servicePager" cusFormatterActions="false"
						ondbClickRowEvent="" actionsWidth="90" showToolbar="false"
						cusDialogSize="normal" showSearchbar="false" showPager="false">
						<jsp:attribute name="searchbar">
				       </jsp:attribute>
						<jsp:body>
				       <isperp:column name="id" title="id" hidden="true"></isperp:column>
				       <isperp:column name="ci_id" title="ci_id" hidden="true"></isperp:column>
				       <isperp:column name="code" title="编号" width="60"
								formatter="return formatCi(cellvalue,options,rowObject);"
								isFormatterFun="true">
				 </isperp:column>
				<isperp:column name="name" title="名称" width="70">
				</isperp:column>
				<isperp:column name="relation_property" title="连接属性" width="180">
				</isperp:column>
				<isperp:column name="treenode_name_path" title="类别" width="90">
				</isperp:column>
			       </jsp:body>
					</isperp:grid>
				</c:otherwise>
			</c:choose>
		</div>
		<div id="relatedCi1"></div>
	</div>

</div>


<isperp:listSelector id="${id}_ciRelateSelector"
	displayValueCssClass="hidden" showClearBtn="false"
	selectorCssClass="hidden" reloadOnOpen="true"
	urlExtScript="'&categoryId='+$('#${id}drpRelationType').val()+'&${id}relationTypeId='+$('#${id}drpRelationType').find('option:selected').attr('rid')+'&ty='+$('#${id}drpRelationType').find('option:selected').attr('ty')+'&ciId='+$('#id').val()"
	url="configuration/ciRelation/listCiEx?a=1"
	afterSelected="${id}_gird_afterSelected(rows)" multiselect="false">
	<isperp:column name="id" title="id" hidden="true"></isperp:column>
	<isperp:column name="code" title="编号" width="80">
	</isperp:column>
	<isperp:column name="name" title="名称" width="100">
	</isperp:column>
	<isperp:column name="status" title="状态" width="60">
	</isperp:column>
	<isperp:column name="conf_category_name" title="类别" width="80">
	</isperp:column>
	<isperp:column name="cus_customer_name" title="客户" width="100">
	</isperp:column>
</isperp:listSelector>


<div id="${id}_relationTypePropertyDlg"  title="关联属性" style="display: none">

</div>

<script type="text/javascript">
    function formatCi(cellvalue,options,rowObject){
       return "<a class='nui-txt-link' style='color:blue;' href=\"javascript:${id}_viewCi('"+rowObject.ci_id+"')\">"+rowObject.code+"</a>";
    }
    function ${id}_viewCi(id){
      
       isperp.openDialog({
			contentUrl : "configuration/ci/edit?id=" + id + "&readonly=t"
		});
    }

	//---根据当前设备类型获取关联---//
	var ciId = $("#id").val();
	var ciCategoryId = "";
	$(function() {
		${id}_loadRelation(ciId);
		
		var viewOnly='${viewOnly}';
		if(viewOnly!='true'){
		  ${id}_loadRelationType();
		}
		
	});
	
	function ${id}_loadRelationType(){
	   categoryId = '${confCategoryId}';
		$("#${id}drpRelationType").html("");
		//根据当前资产类型获取有效的可新增的关系类型
		$.ajax({
			url : "configuration/ciRelation/listCiRelationType",
			data : {
				categoryId : categoryId
			},
			success : function(data) {
				var optionStr = "";
				for ( var i = 0; i < data.length; i++) {
					var ${id}relationTypeId = data[i].id;
					var relationWay = data[i].relation_way;
					var toAliasName = data[i].to_category_alias_name;
					var fromCategoryId=data[i].from_category_id;
					var toCategoryId=data[i].to_category_id;
					var fromTreenodePath=data[i].from_treenode_path;
					var toTreenodePath=data[i].to_treenode_path;
					
					var tvalue="";
					var tName="";
					var ty="";
					if(relationWay=="1"){
					   tName=toAliasName;
					   tvalue=toCategoryId;
					   ty="from_ci";
					}else{
					   tName=data[i].from_category_alias_name;
					   tvalue=fromCategoryId;
					   ty="to_ci";
					}
					
					optionStr += "<option value='"+tvalue+"' data-propertycount='"+data[i].relation_property_count+"' rid='"+${id}relationTypeId+"' ty='"+ty+"'>"+tName+"</option>";
				}
				$("#${id}drpRelationType").append(optionStr);
				$("#${id}drpRelationType").trigger("chosen:updated");
			}
		});
		
	}

	function ${id}_loadRelation(ciId) {
		$("#${id}_tabLiContain").html("");
		$.ajax({
			async : false,
			url : "configuration/ciRelation/listCiRelation",
			data : {
				ciId : ciId
			},
			success : function(data) {
				//alert(data.length);
				//var trStr = "";
				if(data.length == 0){
		 			return;
		 		}
				var relationSelectedIndex=0;
				
				var rId=$("#${id}drpRelationType").find("option:selected").attr("rid");
			 	var liStr="";
			 	
				for ( var i = 0; i < data.length; i++) { 
					
					if(data[i].relation_type_id==rId){
						relationSelectedIndex=i; 
					}
					
					
					//trStr += "<tr><td>" + data[i].name + "</td><td>" + data[i].from_category_id + "</td><td>" + data[i].to_category_id + "</td><td>" + data[i].from_category_alias_name + "</td><td>" + data[i].to_category_alias_name + "</td><td>" + data[i].ci_relation_type + "</td></tr>";
					if (data[i].ci_relation_type == "from_ci") {
						var ${id}relationTypeId=data[i].relation_type_id;
						liStr += "<li class='ui-switchable-item trig-item'><a ryid='"+${id}relationTypeId+"'   id='item_a_" + i + "' href=\"javascript:${id}_selectTab('" + i + "','" + data[i].to_category_id + "','" + data[i].ci_relation_type + "','"+${id}relationTypeId+"');\">" + data[i].to_category_alias_name + "</a></li>";
					} else if (data[i].ci_relation_type == "to_ci") {
						var ${id}relationTypeId=data[i].relation_type_id;
						liStr += "<li class='ui-switchable-item trig-item'><a ryid='"+${id}relationTypeId+"'  id='item_a_" + i + "' href=\"javascript:${id}_selectTab('" + i + "','" + data[i].from_category_id + "','" + data[i].ci_relation_type + "','"+${id}relationTypeId+"');\">" + data[i].from_category_alias_name + "</a></li>";
					}
				}
				$("#${id}_tabLiContain").append(liStr);
				//d$("#tempCiRelationType").append(trStr);
				
				
				if (data[relationSelectedIndex].ci_relation_type == "from_ci") {
					category01 = data[relationSelectedIndex].to_category_id;
					type01 = "from_ci";
					relationTypeId01=data[relationSelectedIndex].relation_type_id;
				} else if ( data[relationSelectedIndex].ci_relation_type == "to_ci") {
					category01 = data[relationSelectedIndex].from_category_id;
					type01 = "to_ci";
					relationTypeId01=data[relationSelectedIndex].relation_type_id;
				} 
	
				${id}_selectTab(relationSelectedIndex, category01, type01,relationTypeId01);

				
				//$("#${id}drpRelationType").append(drpOptionStr);
			}
		});
		 
		
	}
	//categoryId：关联或被关联的资产分类ID
	//type:标示当前资产是主件还是附件，from_ci:主，to_ci：附件
	//ciId:当前资产ID
	function ${id}_selectTab(num, categoryId, type,relationTypeId) {
	    //alert(${id}relationTypeId);
	    $("#${id}relationTypeId").val(relationTypeId);
		$("[class='itmecurr']").attr("class", "");
		$("#item_a_" + num).attr("class", "itmecurr");
		${id }_ciGrid.jqGrid.setGridParam({
			url : "configuration/ciRelation/listRelateCi?categoryId=" + categoryId + "&type=" + type + "&ciId=" + ciId
		});
		${id }_ciGrid.search();
		
		
		$("#${id}drpRelationType").val(categoryId);

		$("#${id}drpRelationType").trigger("chosen:updated");
	}


	function ciUseRelationGridView(rowid) {

		var ciId = ciUseRelationGrid.getRowData(rowid).ci_id;

		openCiView(ciId);
	}

	function openCiView(ciId) {

		var hidKeyword = $("#hidKeyword").val();

		hidKeyword = hidKeyword.replace(/[\ |\~|\`|\!|\@|\#|\$|\%|\^|\&|\*|\(|\)|\-|\_|\+|\=|\||\\|\[|\]|\{|\}|\;|\:|\"|\'|\,|\<|\.|\>|\/|\?]/g, "");

		isperp.openDialog({
			title : "查看资产履历",
			contentUrl : "configuration/ci/edit?id=" + ciId + "&keyword=" + hidKeyword + "&readonly=${readonly }"
		});

	}
	function openCiEdit(ciId) {
		isperp.openDialog({
			title : "编辑资产",
			contentUrl : "configuration/ci/edit??changeId=${changeId}&id=" + ciId
		});

	}

	function checkSelectedCi() {

		var reccount = ciUseRelationGrid.jqGrid.getGridParam("reccount");
		if (reccount == 0) {
			isperp.errorAlert("请添加关联资产！");
			return false;
		}
		return true;
	}
	$("#${id}addRelateCiDialog").dialog({
		title : "选择类型",
		width : 350,
		height : 150,
		modal : true,
		autoOpen : false,
		resizable : false,
		buttons : [ {
			text : "确定",
			"class" : 'submit nui-mainbtn',
			click : function() {
				$("#${id}addRelateCiDialog").dialog("close");
				$("#${id}_ciRelateSelector").click();
			}

		}, {
			text : "关闭",
			click : function() {

				$(this).dialog("close");
			}
		} ]

	});
	function ${id}_addRelation() {
		
		$("#${id}_ciRelateSelector").click();
	}

	function ${id}_gird_afterSelected(rows) {
		if (rows.length < 1) {
			isperp.alert("没有数据被选择！")
			return;
		}
		var rId=$("#${id}drpRelationType").find("option:selected").attr("rid");
		var ty=$("#${id}drpRelationType").find("option:selected").attr("ty");
		var pcount=$("#${id}drpRelationType").find("option:selected").data("propertycount");
		var ids = "";
		var turl="";
		var tfromId="";
		var ttoId="";
		for(var i=0;i<rows.length;i++){
		   if(i==0){
		      ids=rows[0].id;
		   }else{
		      ids+=","+rows[i].id;
		   }
		}
		//当前资产是主件
		if(ty=="from_ci"){
		   turl="configuration/ciRelation/saveCiRelation";
		   tfromId=ciId;
		   ttoId=ids;
		}else{//当前资产是附件
		   turl="configuration/ciRelation/saveCiRelationIsToCi";
		   tfromId=ids;
		   ttoId=ciId;
		}
		$.ajax({
			url : turl,
			async:false,
			cache:false,
			data : {
				fromId : tfromId,
				relationType : rId,
				toCiIds : ttoId
			},
			success : function(dataResp) {
				 
				if(pcount>0){
					
					
					$.ajax({
						url : "configuration/ciRelationTypeProperty/listByRelationType",
						async:false,
						cache:false,
						data : {
							relationTypeId:rId
						},
						success:function(data){
							
							var html="<div  style='padding:10px 15px'><form action='configuration/ciRelationProperty/saveProperties' class='validate' id='${id}_relationTypePropertyForm'  method='post' >";
							html+="<input name='relationTypeId' value='"+rId+"' type='hidden'>";
							html+="<br><input name='relationId' value='"+dataResp.data+"' type='hidden'>";
							$(data).each(function(index,item){
								html+=item.name+"：<input name='relationTypePropertyName' value='"+item.name+"'  type='hidden'><input type='text'  name='relationTypePropertyValue' class='required'><br><br>";
							});
								
							html+="</form></div>";
							
							
							$("#${id}_relationTypePropertyDlg").dialog({
								resizable : false,
								modal : true,
								show : true,
								hide : false,
								width:360,
								height:200,
								buttons : {
									"确定" : function() {
										 $("#${id}_relationTypePropertyForm").ajaxSubmit({
											 
											 success:function(){
												 $("#${id}_relationTypePropertyDlg").dialog('close');
													${id}_loadRelation(ciId);
											 }
										 });
									}
								}
							});
							$("#${id}_relationTypePropertyDlg").html(html);
							
						}
					})
					
					

				}else{

					${id}_loadRelation(ciId);
				}

			},
			error : function(data) {
				isperp.alert(data)
			}
		});

	}
	
	
	
	
</script>